/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */package org.merak.core.text.json;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.merak.core.text.json.jsonfiers.Jsonfier;

public final class TemplateProperty {

    //~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private String   name     = null; // mandatory, immutable
	private Method   getter   = null; // mandatory, immutable
	private Method   setter   = null; // mandatory, immutable
	private Jsonfier jsonfier = null; // optional

    //~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	protected TemplateProperty(String name, Method getter, Method setter) {
		this.name     = name;
		this.getter   = getter;
		this.setter   = setter;
	}

    //~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	/** Return the property's name */
	public final String getName() {
		return this.name;
	}
	//****************************************************************************
	/** Executes the getter, getting the value of the property in the given object */
	public final Object getValue(Object obj)
		throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
	{
		return this.getter.invoke(obj);
	}
	//****************************************************************************
	/** Executes the setter, setting the value of the property in the given object */	
	public final void setValue(Object obj,Object value)
		throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
	{
		this.setter.invoke(obj,value);
	}
	//****************************************************************************
	/** Gets the jsonfier used to render this property, if any */
	public final Jsonfier getJsonfier() {
		return jsonfier;
	}
	//****************************************************************************
	/** Changes the jsonfier used to render this property, if any */
	public final void setJsonfier(Jsonfier jsonfier) {
		this.jsonfier = jsonfier;
	}
	//****************************************************************************
	/** Checks if this property has a predefined jsonfier */
	public final boolean hasJsonfier() {
		return this.jsonfier != null;
	}		
	//****************************************************************************

}
